using System.Collections;

namespace UnityEngine.Recorder
{
	[ExecuteInEditMode]
	public class RecorderComponent : MonoBehaviour
	{
		public bool autoExitPlayMode
		{
			get;
			set;
		}

		public RecordingSession session
		{
			get;
			set;
		}

		public void Update()
		{
			if (session != null && session.recording)
			{
				session.PrepareNewFrame();
			}
		}

		private IEnumerator RecordFrame()
		{
			yield return new WaitForEndOfFrame();
			if (session == null || !session.recording)
			{
				yield break;
			}
			session.RecordFrame();
			switch (session.m_Recorder.settings.m_DurationMode)
			{
			case DurationMode.SingleFrame:
				if (session.m_Recorder.recordedFramesCount == 1)
				{
					base.enabled = false;
				}
				break;
			case DurationMode.FrameInterval:
				if (session.frameIndex > session.settings.m_EndFrame)
				{
					base.enabled = false;
				}
				break;
			case DurationMode.TimeInterval:
			{
				if (session.settings.m_FrameRateMode == FrameRateMode.Variable)
				{
					if (session.m_CurrentFrameStartTS >= (double)session.settings.m_EndTime)
					{
						base.enabled = false;
					}
					break;
				}
				double num = (double)(session.settings.m_EndTime - session.settings.m_StartTime) * session.settings.m_FrameRate;
				if ((double)session.RecordedFrameSpan >= num)
				{
					base.enabled = false;
				}
				break;
			}
			}
		}

		public void LateUpdate()
		{
			if (session != null && session.recording)
			{
				StartCoroutine(RecordFrame());
			}
		}

		public void OnDisable()
		{
			if (session != null)
			{
				session.Dispose();
				session = null;
			}
		}

		public void OnDestroy()
		{
			if (session != null)
			{
				session.Dispose();
			}
		}
	}
}
